﻿using System;
using System.ComponentModel;
using VNet.VisiSuite.Common.Resource;
using VNet.VisiSuite.VisiAdmin.Common;
using VNet.VisiSuite.VisiAdmin.Common.Helpers;

namespace VNet.VisiSuite.VisiAdmin.Models
{
    public class AlarmSettingModel : ComparableBaseModel<AlarmSettingModel>, IDataErrorInfo
    {
        private int _alarmSettingId;
        public int AlarmSettingId
        {
            get { return _alarmSettingId; }
            set
            {
                _alarmSettingId = value;
                OnPropertyChanged("AlarmSettingId");
            }
        }

        private int? _areaId;
        public int? AreaId
        {
            get { return _areaId; }
            set
            {
                _areaId = value;
                OnPropertyChanged("AreaId");
            }
        }

        private string _colorString;
        [IgnoreClone]
        public string ColorString
        {
            get { return _colorString; }
            set
            {
                _colorString = string.Empty;
                var color = System.Drawing.Color.White;
                try
                {
                    color = System.Drawing.Color.FromName(value);
                    if (color.A == 0 && color.R == 0 && color.G == 0 && color.B == 0)
                    {
                        color = System.Drawing.ColorTranslator.FromHtml(value);
                        if (color.A == 0 && color.R == 0 && color.G == 0 && color.B == 0)
                            _colorString = Constants.WhiteColorHexString;
                    }
                }
                catch
                {
                    _colorString = Constants.WhiteColorHexString;
                }

                if (string.IsNullOrEmpty(_colorString))
                    _colorString = value != null ? value.ToUpper() : null;
                OnPropertyChanged("ColorString", true);

                _color = System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B);
                OnPropertyChanged("Color");
            }
        }

        private System.Windows.Media.Color _color;
        [IgnoreClone]
        [Category("Properties")]
        [DisplayName("Color")]
        [Description("Alarm Setting Color.")]
        public System.Windows.Media.Color Color
        {
            get { return _color; }
            set
            {
                _color = value;
                OnPropertyChanged("Color");

                System.Drawing.Color newColor = System.Drawing.Color.FromArgb(_color.A, _color.R, _color.G, _color.B);
                _colorString = (_color.A == 0 && _color.R == 0 && _color.G == 0 && _color.B == 0) ? null : string.Format("#{0}", newColor.Name);
                OnPropertyChanged("ColorString", true);
            }
        }

        private short _minPercentageGood;
        [Category("Properties")]
        [DisplayName("Min Percentage Good (%)")]
        [Description("Alarm Setting Min Percentage Good (%).")]
        public short MinPercentageGood
        {
            get { return _minPercentageGood; }
            set
            {
                _minPercentageGood = value;
                OnPropertyChanged("MinPercentageGood",true);
            }
        }

        private string _createdBy;
        [Category("Update History")]
        [DisplayName("Created By")]
        [Description("User who created the alarm setting in VisiAdmin.")]
        public string CreatedBy
        {
            get { return _createdBy; }
            set
            {
                _createdBy = value;
                OnPropertyChanged("CreatedBy");
            }
        }

        private string _createdDate;
        [Category("Update History")]
        [DisplayName("Created On")]
        [Description("Date when the alarm setting was created in VisiAdmin.")]
        public string CreatedDate
        {
            get { return _createdDate; }
            set
            {
                _createdDate = value;
                OnPropertyChanged("CreatedDate");
            }
        }

        private string _lastUpdatedBy;
        public string LastUpdatedBy
        {
            get { return _lastUpdatedBy; }
            set
            {
                _lastUpdatedBy = value;
                OnPropertyChanged("LastUpdatedBy");
            }
        }

        private string _lastUpdatedDate;
        [Category("Update History")]
        [DisplayName("Updated On")]
        [Description("Date when the alarm setting was last edited in VisiAdmin.")]
        public string LastUpdatedDate
        {
            get { return _lastUpdatedDate; }
            set
            {
                _lastUpdatedDate = value;
                OnPropertyChanged("LastUpdatedDate");
            }
        }

        private string _fontColorString;
        [IgnoreClone]
        public string FontColorString
        {
            get { return _fontColorString; }
            set
            {
                _fontColorString = string.Empty;
                var fontColor = System.Drawing.Color.White;
                try
                {
                    fontColor = System.Drawing.Color.FromName(value);
                    if (fontColor.A == 0 && fontColor.R == 0 && fontColor.G == 0 && fontColor.B == 0)
                    {
                        fontColor = System.Drawing.ColorTranslator.FromHtml(value);
                        if (fontColor.A == 0 && fontColor.R == 0 && fontColor.G == 0 && fontColor.B == 0)
                            _fontColorString = Constants.WhiteColorHexString;
                    }
                }
                catch
                {
                    _fontColorString = Constants.WhiteColorHexString;
                }

                if (string.IsNullOrEmpty(_fontColorString))
                    _fontColorString = value != null ? value.ToUpper() : null;
                OnPropertyChanged("FontColorString", true);

                _fontColor = System.Windows.Media.Color.FromArgb(fontColor.A, fontColor.R, fontColor.G, fontColor.B);
                OnPropertyChanged("FontColor");
            }
        }

        private System.Windows.Media.Color _fontColor;
        [IgnoreClone]
        [Category("Properties")]
        [DisplayName("Font Color")]
        [Description("Alarm Setting Font Color.")]
        public System.Windows.Media.Color FontColor
        {
            get { return _fontColor; }
            set
            {
                _fontColor = value;
                OnPropertyChanged("FontColor");

                System.Drawing.Color newFontColor = System.Drawing.Color.FromArgb(_fontColor.A, _fontColor.R, _fontColor.G, _fontColor.B);
                _fontColorString = (_fontColor.A == 0 && _fontColor.R == 0 && _fontColor.G == 0 && _fontColor.B == 0) ? null : string.Format("#{0}", newFontColor.Name);
                OnPropertyChanged("FontColorString", true);
            }
        }

        private short _minPercentageBad;
        [Category("Properties")]
        [DisplayName("Min Percentage Bad (%)")]
        [Description("Alarm Setting Min Percentage Bad (%).")]
        public short MinPercentageBad
        {
            get { return _minPercentageBad; }
            set
            {
                _minPercentageBad = value;
                OnPropertyChanged("MinPercentageBad", true);
            }
        }

        #region IDataErrorInfo Implementation

        public string Error
        {
            get { return string.Empty; }
        }

        public string this[string columnName]
        {
            get
            {
                if (!ShouldValidate)
                    return string.Empty;

                if (columnName == "Color" && String.IsNullOrWhiteSpace(_colorString))
                    return ValidationMessages.ColorRequired;
                if (columnName == "FontColor" && String.IsNullOrWhiteSpace(_fontColorString))
                    return ValidationMessages.FontColorRequired;
                if (columnName == "MinPercentageGood" && (MinPercentageGood > 100 || MinPercentageGood < 0))
                    return ValidationMessages.PercentageError;
                if (columnName == "MinPercentageBad" && (MinPercentageBad > 100 || MinPercentageBad < 0))
                    return ValidationMessages.PercentageError;
                return string.Empty;
            }
        }

        #endregion

        #region Override Equals methods
        public bool Equals(AlarmSettingModel other)
        {
            if (ReferenceEquals(null, other))
                return false;
            if (ReferenceEquals(this, other))
                return true;

            return string.Equals(ColorString, other.ColorString)
                && string.Equals(FontColorString, other.FontColorString)
                && MinPercentageBad == other.MinPercentageBad
                && MinPercentageGood == other.MinPercentageGood;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
                return false;
            if (ReferenceEquals(this, obj))
                return true;
            if (obj.GetType() != this.GetType())
                return false;
            return Equals((AlarmSettingModel)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                var result = 0;
                result = (result * 397) ^ (ColorString ?? string.Empty).GetHashCode();
                result = (result * 397) ^ (FontColorString ?? string.Empty).GetHashCode();
                result = (result * 397) ^ MinPercentageBad.GetHashCode();
                result = (result * 397) ^ MinPercentageGood.GetHashCode();
                return result;
            }
        }
        #endregion
    }
}
